import Vue from 'vue'
import App from './App'
import store from './store'
import Cache from './utils/cache'
import util from 'utils/util'
import configs from './config/app.js'
import Vant from 'vant';
import uView from "uview-ui";



Vue.prototype.$util = util;
Vue.prototype.$config = configs;
Vue.prototype.$Cache = Cache;
Vue.prototype.$eventHub = new Vue();
Vue.config.productionTip = false
Vue.use(Vant);
Vue.use(uView);




//日期添加格式化方法
Date.prototype.Format = function (fmt) { //author: meizz
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
};

// #ifdef H5
import { parseQuery } from "./utils";
import Auth from './libs/wechat';
import { SPREAD } from './config/cache';
Vue.prototype.$wechat = Auth;
let cookieName = "VCONSOLE",
	query = parseQuery(),
	urlSpread = query["spread"],
	vconsole = query[cookieName.toLowerCase()],
	md5Crmeb = "b14d1e9baeced9bb7525ab19ee35f2d2", //CRMEB MD5 加密开启vconsole模式
	md5UnCrmeb = "3dca2162c4e101b7656793a1af20295c"; //UN_CREMB MD5 加密关闭vconsole模式

if (urlSpread !== undefined) {
	var spread = Cache.get(SPREAD);
	urlSpread = parseInt(urlSpread);
	if (!Number.isNaN(urlSpread) && spread !== urlSpread) {
		Cache.set("spread", urlSpread || 0);
	} else if (spread === 0 || typeof spread !== "number") {
		Cache.set("spread", urlSpread || 0);
	}
}

if (vconsole !== undefined) {
  if (vconsole === md5UnCrmeb && Cache.has(cookieName))
	  Cache.clear(cookieName);
} else vconsole = Cache.get(cookieName);

import VConsole from './components/vconsole.min.js'

if (vconsole !== undefined && vconsole === md5Crmeb) {
	Cache.set(cookieName, md5Crmeb, 3600);
	let vConsole = new VConsole();
}

Auth.isWeixin() && Auth.oAuth();

// #endif

// H5 plus事件处理


App.mpType = 'app'

import i18n from './lang/index' 
Vue.prototype._i18n = i18n

const app = new Vue({
    i18n,
	...App,
	store,
	Cache
})
app.$mount();



document.addEventListener('plusready', function (a) {
	let first = null;
	plus.key.addEventListener('backbutton', function () {
		//获取地址栏目中的url
		let urls = location.hash.split('/')[1]
		
		if (urls === 'map' || urls === '') {//判断当前是否无后退页
			if (new Date().getTime() - first < 3000) {
				// 连续按两次返回键且时间小于3s，退出app
				plus.runtime.quit();
			}else{
				first = new Date().getTime();//记录第一次按返回键的时间
				if (confirm("点击确定退出应用")) {
					plus.runtime.quit()
				}
				setTimeout(()=>{
					// 3s后清除
					first = null;
				}, 1000);
			}
		}else{
			uni.navigateBack();
		}
	}, false);
   //设置状态栏颜色
   plus.navigator.setStatusBarBackground("rgb(246, 202, 79)");
   setTimeout(function(){
	  plus.navigator.setStatusBarBackground("rgb(246, 202, 79)"); 
   },3000);
});

